package commands;

import indexes.BattingIndex;
import indexes.PlayerIDIndex;
import indexes.PlayerNameIndex;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

import util.BattingDatabaseFile;
import util.DatabaseFile;
import util.PlayerLogger;
import util.Record;

import main.PlayerDB;
/**
 * The import command parses a file (given in the arguments)
 * parses it, and adds the player records to the database file
 * as well as the index structures.
 * @author bfults (Brian Fults - 905084698)
 *
 */
public class ImportCommand extends Command
{
    /**
     * Typical constructor.
     * @param args
     */
    public ImportCommand(List<String> args)
    {
        super(args);
    }
    

    @Override
    public boolean execute(
        PlayerIDIndex idIndex,
        PlayerNameIndex nameIndex,
        PlayerLogger logger,
        DatabaseFile database, BattingIndex battingIndex, BattingDatabaseFile battingDatabase ) throws IOException
    {
        logger.write(super.getDivider() + "\n");
        logger.write( "Command: import " + getArguments().get(0) + "\n\n");
        List<Record> imported = database.importRecords(getArguments().get(0));
        for (Record record: imported)
        {
            idIndex.add(record.getId(), record.getOffset());
            nameIndex.add(record.getName(), record.getOffset());
        }
        logger.write("Imported: " + imported.size() + " records.\n");
        return true;
    }
 


}
